package com.icss.daos;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.icss.beans.NewsVO;
import com.icss.utils.DB;

/**
 * @author 廖鹏
 * @version 创建时间：2010-3-26 下午07:52:32
 * 类说明 前台的新闻管理DAO
 */
public class NewsDao {
	
	private Dao dao=null;
	private ResultSet rSet=null;
	
	public NewsDao(){
		
	}
	
	/**
	 * @author 廖鹏
	 * 功能：最近10条新闻
	 * @return
	 */
	public List<NewsVO> getRecently(){
		dao=new Dao();
		StringBuffer sqlBuffer=new StringBuffer();
		sqlBuffer.append("select * from news order by publishTime desc limit 0,10");
		rSet=dao.query(sqlBuffer.toString());
		List<NewsVO> newsList=new ArrayList<NewsVO>();
		try {
			while(rSet.next()){
				NewsVO newsVO=new NewsVO();
				newsVO.setNewsID(rSet.getInt("newsID"));
				newsVO.setNewsTitle(rSet.getString("newsTitle"));
				newsVO.setNewsContent(rSet.getString("newsContent"));
				newsVO.setPublishTime(rSet.getDate("publishTime"));
				newsList.add(newsVO);
			}
			dao.close();
			return newsList;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 功能：新闻分页查询
	 * @param currentPage
	 * @param pageSize
	 * @param condition
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<NewsVO> getPageNews(int currentPage,int pageSize,String condition){
		Connection connection=DB.getConnection();
		List list=new ArrayList();
		try {
			CallableStatement caStatement = connection
					.prepareCall("{call proc_pageable(?,?,?,?,?,?,?,?,?)}");
			caStatement.setString(3, "news");
			caStatement.setString(4, "*");
			caStatement.setString(5, "publishTime");
			caStatement.setString(6, condition);
			caStatement.setInt(7, pageSize);
			caStatement.setInt(8, currentPage);
			caStatement.setInt(9, 0);
			caStatement.registerOutParameter(1, java.sql.Types.INTEGER);
			caStatement.registerOutParameter(2, java.sql.Types.INTEGER);
			rSet = caStatement.executeQuery();
			List<NewsVO> newsList = new ArrayList<NewsVO>();
			while (rSet.next()) {
				NewsVO newsVO = new NewsVO();
				newsVO.setNewsID(rSet.getInt("newsID"));
				newsVO.setNewsTitle(rSet.getString("newsTitle"));
				newsVO.setNewsContent(rSet.getString("newsContent"));
				newsVO.setPublishTime(rSet.getDate("publishTime"));
				newsList.add(newsVO);
			}
			list.add(0, newsList);
			list.add(1, caStatement.getInt(1));
			list.add(2, caStatement.getInt(2));
		} catch (Exception e) {
		}finally{
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
	
	
	/**
	 * @author 廖鹏
	 * 功能：添加新闻
	 * @param newsVO
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public  boolean addNew(NewsVO newsVO){
		dao=new Dao();
		StringBuffer sqlBuffer=new StringBuffer();
		sqlBuffer.append("insert into news(newsTitle,newsContent,publishTime) values(");
		sqlBuffer.append("'").append(newsVO.getNewsTitle()).append("','")
			.append(newsVO.getNewsContent()).append("','").append(newsVO.getPublishTime().toLocaleString()).append("')");
		boolean b=dao.execute(sqlBuffer.toString());
		dao.close();
		return b;
	}
	
	/**
	 * @author 廖鹏
	 * 功能：删除一条新闻
	 * @param newsID
	 * @return
	 */
	public boolean delNotice(int newsID){
		dao=new Dao();
		StringBuffer sqlBuffer=new StringBuffer();
		sqlBuffer.append("delete from news where newsID=").append(newsID);
		boolean b=dao.execute(sqlBuffer.toString());
		dao.close();
		return b;
	}
	
}
